package cn.wolfcode.shop.cloud.service.impl;

import cn.wolfcode.shop.cloud.BusinessException;
import cn.wolfcode.shop.cloud.domain.OrderInfo;

import cn.wolfcode.shop.cloud.mapper.OrderInfoMapper;
import cn.wolfcode.shop.cloud.mq.MQConstans;
import cn.wolfcode.shop.cloud.msg.SeckillGoodsCodeMsg;
import cn.wolfcode.shop.cloud.service.IOrderInfoService;
import cn.wolfcode.shop.cloud.service.ISeckillGoodService;
import cn.wolfcode.shop.cloud.service.ISeckillOrderService;

import cn.wolfcode.shop.cloud.service.RedisService;
import cn.wolfcode.shop.cloud.util.IdGenerateUtil;
import cn.wolfcode.shop.cloud.vo.SeckillGoodVO;

import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Service
public class OrderInfoServiceImpl implements IOrderInfoService {

    @Autowired
    private ISeckillGoodService seckillGoodService;
    @Autowired
    private ISeckillOrderService seckillOrderService;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private RedisService redisService;


    @Override
    @Transactional
    public String seckill(Long userId, Long seckillId) {
        //6 具体秒杀的业务
        //  6.1  减少库存 -1
        int count = seckillGoodService.decrCount(seckillId);
        if(count == 0){
            throw  new BusinessException(SeckillGoodsCodeMsg.SECKILL_OVER);
        }
        //  6.2  创建订单信息
        String orderNo = createOrderInfo(userId,seckillId);
        //  6.3  创建秒杀订单信息
        seckillOrderService.createSeckillOrder(userId,seckillId,orderNo);
        return orderNo;
    }

    @Override
    public OrderInfo find(String orderNo) {

        return orderInfoMapper.find(orderNo);

    }

    @Override
    public void cancelOrder(String orderNo, Long goodId) {
        //取消订单操作
        //1 修改定的姿态为超时未支付 where status = 0
        int count = orderInfoMapper.cancelOrder(orderNo);
        if(count>0){
            //添加真实库存
            seckillGoodService.incrStock(goodId);
            //回补预库存
            seckillGoodService.syncStock(goodId);
            //发送广播通知 修改本地标识
            rocketMQTemplate.syncSend(MQConstans.CREATE_ORDER_FAIL_NOTIFY_DEST,goodId);

        }
    }

    @Override
    public int updateStatus(String orderNo, Integer statusAccountPaid) {
        int count = orderInfoMapper.updateStatus(orderNo,statusAccountPaid);
        if(count<0){
            //走退款流程
        }
        return count;

    }


    private String createOrderInfo(Long userId, Long seckillId) {
        SeckillGoodVO seckillGoodVO = seckillGoodService.find(seckillId);
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setCreateDate(new Date());
        orderInfo.setGoodCount(seckillGoodVO.getStockCount());
        orderInfo.setGoodId(seckillGoodVO.getGoodId());
        orderInfo.setGoodImg(seckillGoodVO.getGoodImg());
        orderInfo.setGoodName(seckillGoodVO.getGoodName());
        orderInfo.setGoodPrice(seckillGoodVO.getGoodPrice());
        orderInfo.setOrderNo(IdGenerateUtil.get().nextId()+"");
        orderInfo.setSeckillPrice(seckillGoodVO.getSeckillPrice());
        orderInfo.setUserId(userId);
        orderInfoMapper.save(orderInfo);

        return orderInfo.getOrderNo();
    }
}
